/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ep.app;

import com.vaadin.ui.Alignment;
import com.vaadin.ui.CustomLayout;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Window;
import ep.libs.implementations.AbstractApplication;
import ep.libs.implementations.AbstractModule;
import ep.m001.module.M001Module;

/**
 *
 * @author Spyros
 */
public class MainApplication extends AbstractApplication {

    private Window mainWindow;
    private VerticalLayout mainLayout;
    private HorizontalLayout headerLayout;

    @Override
    public void init() {
        initializeApplication();
    }

    // <editor-fold defaultstate="collapsed" desc="getters setters">
    @Override
    public VerticalLayout getMainLayout() {
        return mainLayout;
    }

    @Override
    public HorizontalLayout getHeaderLayout() {
        return headerLayout;
    }

    @Override
    public Window getApplicationWindow() {
        return mainWindow;
    }
    // </editor-fold>

    private void initializeApplication() {
        mainWindow = new Window();
        setMainWindow(mainWindow);
        setTheme("apptheme");
        CustomLayout customLayout = new CustomLayout("ePre2");
        mainWindow.setContent(customLayout);
        mainLayout = new VerticalLayout();
        headerLayout = new HorizontalLayout();
        headerLayout.setSizeUndefined();
        headerLayout.setSpacing(true);
        VerticalLayout mainWindowLayout = new VerticalLayout();
        mainWindowLayout.setMargin(true);
//        mainWindowLayout.addComponent(headerLayout);
        customLayout.addComponent(headerLayout, "account");
//        mainWindowLayout.setComponentAlignment(headerLayout, Alignment.TOP_RIGHT);
        mainWindowLayout.addComponent(mainLayout);
        customLayout.addComponent(mainWindowLayout, "maincontent");

        changeModule(M001Module.class);
    }

    @Override
    public void changeModule(Class moduleClass) {
        changeModule(moduleClass, null);
    }

    @Override
    public void changeModule(Class moduleClass, Object[] args) {
        try {
            AbstractModule module = (AbstractModule) moduleClass.getConstructor(AbstractApplication.class, Object[].class).newInstance(this, args);
            getMainLayout().removeAllComponents();
            getMainLayout().addComponent(module.getModule());
        } catch (Exception ex) {
            ex.printStackTrace(System.out);
        }
    }
}
